package com.yc.api2;

import java.util.*;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/11/1
 * Email haijunzhou@hnit.edu.cn
 */
public class CollectionDemo {

    public static void main(String[] args) {
        int i02 = 59;
        Integer i04 = new Integer(59);
        System.out.println(i02 == i04);
    }

    public static void main1(String[] args) {
//        list 有序 => 通过序号(下标)访问元素
//        set  无序 => 无法通过下标访问, 不方便访问元素
//        map : 为set集合中的元素命名 => 变成了 map 集合
        Map<String, Student> map = new HashMap<>();
        map.put(null, new Student("NULL",11));
        map.put("001", new Student("张三",12));
        map.put("002", new Student("李四",13));
        map.put("003", new Student("王五",14));
        map.put("004", new Student("赵六",15));
        map.put("005", new Student("赵六",15));

        Student wangwu = map.get("003");
        System.out.println("wangwu = " + wangwu);

//        list => for( int i=0 ....)
//        set => for( obj : set)
        // map的遍历
        // 遍历键   键不能重复
        Set<String> keySet = map.keySet();
        for (String sn : keySet) {
            System.out.println("sn = " + sn);
        }
        // 遍历值   值可以重复
        Collection<Student> values = map.values();
        for (Student student : values) {
            System.out.println("student = " + student);
        }

        // Entry 是 Map 的静态内部类, 包装 键 和 值
        Set<Map.Entry<String, Student>> entries = map.entrySet();
        for (Map.Entry<String, Student> entry : entries) {
            String key = entry.getKey();
            Student value = entry.getValue();
            System.out.printf("key = %s, value = %s \n", key, value);
        }


    }

    public static void compare(String[] args) {

        List<Student> list = new ArrayList<>();

        list.add(new Student("赵六",11));
        list.add(new Student("张三",18));
        list.add(new Student("王五",22));
        list.add(new Student("李四",28));
        // 只要内部元素实现了排序接口, 那么就可以使用 sort 进行排序
        Collections.sort(list);

        for (Student student : list) {
            System.out.println("student = " + student);
        }

        // 安装姓名字符串排序
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.name.compareTo(o2.name);
            }
        });
        System.out.println("=----------------------------------");
        for (Student student : list) {
            System.out.println("student = " + student);
        }
    }

    public static void set(String[] args) {
        // list: 有序可重复,  下标访问
        // set: 无序不重复,    不提供下标访问
        // Set<Integer> set = new HashSet<>();
        // LinkedHashSet 有序 不重复
        //Set<Integer> set = new LinkedHashSet<>();
        // TreeSet 排序 不重复
        Set<Integer> set = new TreeSet<>();
        set.add(1000);
        set.add(1000);
        set.add(5000);
        set.add(2000);
        set.add(3000);
        set.add(4000);

        System.out.println("元素个数 set.size() = " + set.size());

        // 增强for循环
        for( Integer i : set){
            System.out.println("i = " + i);
        }

        System.out.println("=============================");

        // HashSet 无序不重复
        // Set<Student> set1 = new HashSet<>();
        // LinkedHashSet 有序不重复
        // Set<Student> set1 = new LinkedHashSet<>();
        // TreeSet 排序不重复, 要求存入的元素必须实现排序接口
        Set<Student> set1 = new TreeSet<>();
        // equals;  ==
        set1.add(new Student("张三", 10));
        set1.add(new Student("王五", 21));
        set1.add(new Student("张三", 10));
        set1.add(new Student("赵六", 13));
        set1.add(new Student("李四", 11));
        set1.add(new Student("马七", 16));

        System.out.println("set1.size() = " + set1.size());

        for (Student student : set1) {
            System.out.println("student = " + student);
        }


    }

    public static void list(String[] args) {
        // 没加泛型
        List list = new ArrayList();  // 底层是数组实现
        list.add(1);
        list.add(1);
        list.add(2);
        list.add(3);

        System.out.println("list.get(0) = " + list.get(0));
        System.out.println("list.get(1) = " + list.get(1));

        for (int i = 0; i < list.size(); i++) {
            System.out.println("list.get(i) = " + list.get(i));
        }

        // 没有泛型获取的元素, 默认 object 类型
        list.add("张三");
        list.add(true);
        list.add(2,"你好");
        Object o = list.get(2);
        System.out.println("o.getClass() = " + o.getClass());
        System.out.println("o = " + o);
        // 必须转型才能使用 String 的函数
        String s = (String)list.get(2);
        System.out.println("s.length() = " + s.length());

        // 使用泛型创建集合, 必须Object类型和子类, 不能使用基本数据类型
        List<Integer> list2 = new LinkedList<>();
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            // list2.add("字符串"); // 报错
            list2.add(random.nextInt(101));
        }

        int sum = 0;
        for (int i = 0; i < list2.size(); i++) {
            Integer score = list2.get(i);
            System.out.println("score = " + score);
            sum += score;
        }
        int avg = sum / list2.size();
        System.out.println("平均分:" + avg);
    }
}

class Student implements Comparable<Student>{
    String name;
    int age;

    // alt + insert


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}
